package com.lanou3g.ptp.approval.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.approval.mapper.ApprovalMapper;
import com.lanou3g.ptp.entity.Approval;
import com.lanou3g.ptp.entity.Message;
import com.lanou3g.ptp.entity.Student;
import com.lanou3g.ptp.message.service.MessageService;
import com.lanou3g.ptp.service.ApprovalService;
import com.lanou3g.ptp.signInDate.service.SignInDateService;
import com.lanou3g.ptp.student.service.StudentService;
import com.lanou3g.ptp.taskstudent.service.TaskStudentService;
import com.lanou3g.ptp.vo.Pageable;
import com.lanou3g.ptp.vo.TemplateData;
import com.lanou3g.ptp.vo.WxTmplMsg;
import com.lanou3g.ptp.wx.service.WxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(timeout = 5000, interfaceClass = ApprovalService.class)
public class ApprovalServiceImpl implements ApprovalService {

    @Autowired
    private ApprovalMapper approvalMapper;

    @Reference
    private StudentService studentService;

    @Reference
    private WxService wxService;

    @Reference
    private MessageService messageService;

    @Reference
    private SignInDateService signInDateService;

    @Reference
    private TaskStudentService taskStudentService;

    //老师接口查询所有请假信息
    @Override
    public PageInfo<Approval> unPassApprovals(Long teacherId, Pageable page) {
        PageHelper.startPage(page.getPage(), page.getSize());
        //查询所有请假信息
        List<Approval> unPassItems = approvalMapper.findUnPassByTeacherId(teacherId);
        return PageInfo.of(unPassItems);
    }

    @Override
    public List<Approval> findByStudentId(Long studentId) {
        return approvalMapper.findByStudentId(studentId);
    }


    @Override
    @Transactional
    public Boolean passApproval(String teacherName, Long approvalId) {
        Approval approval = this.approvalMapper.fingApprovalMsgByApprovalId(approvalId);
        if (approval.getStatus() == 0) {
            Long studentId = approval.getStudentId();
            Student student = this.studentService.studentById(studentId);
            String openId = student.getOpenId();
            return wxMessageApprovalInsert(student, approval, teacherName, 1);
        }
        return false;
    }

    //////////////////拒绝
    @Override
    @Transactional
    public Boolean denyApproval(String teacherName, Long approvalId) {

        Approval approval = this.approvalMapper.fingApprovalMsgByApprovalId(approvalId);
        if (approval.getStatus() == 0) {
            Long studentId = approval.getStudentId();
            Student student = this.studentService.studentById(studentId);
            String openId = student.getOpenId();
            return wxMessageApprovalInsert(student, approval, teacherName, 2);
        }
        return false;

    }

    private String changeDateToString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        return simpleDateFormat.format(date);
    }

    private Boolean wxMessageApprovalInsert(Student student, Approval approval, String teacherName, int status) {
        Date now = new Date();
        String openId = student.getOpenId();

        String content = "";
        if (status == 1) {
            content = "你提交的请假申请已通过";
        }
        if (status == 2) {
            content = "你提交的请假申请未通过";
        }
        if (openId != null && !openId.trim().equals("")) {

            Map<String, TemplateData> data = new HashMap<>();

            //审核结果
            data.put("keyword1", new TemplateData(content));
            //审核人员
            data.put("keyword2", new TemplateData(teacherName));
            //拒绝理由

            //审核时间
            data.put("keyword4", new TemplateData(changeDateToString(now)));
            //申请时间
            data.put("keyword5", new TemplateData(changeDateToString(approval.getCreateTime())));
            //备注

            boolean result = wxService.sendMessage(openId, WxTmplMsg.TYPE_APPROVAL, data);


            Message message = new Message();
            message.setContent(content);
            message.setStudentId(student.getId());
            message.setTaskId(approval.getTaskId());
            messageService.saveApprovalMessage(message);
            int i = this.approvalMapper.upStatus(approval.getId(), teacherName, now, status);
            return i > 0;

        }
        return false;
    }

    @Override
    public boolean postApproval(Approval approval) {
        if (validApproval(approval)) {
            Date now = new Date();
            approval.setCreateTime(now);
            approval.setUpdateTime(now);
            approval.setStatus(0);
            int i = approvalMapper.insert(approval);
            return i > 0;
        }
        return false;
    }

    private boolean validApproval(Approval approval) {
        Long taskId = approval.getTaskId();
        boolean ifDateInSignInDates = signInDateService.ifDateInSignInDates(approval.getStartTime(), taskId);
        boolean ifStudentInTask = taskStudentService.ifStudentInTask(approval.getStudentId(), taskId);
        return ifDateInSignInDates && ifStudentInTask;
    }


}
